﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SHJunketingModel;
using System.Data.SqlClient;

namespace SHJunketingDAL
{
    public class HospitalDal
    {
        public static DataTable GetJigou()
        {
            string safeSql = "select * from hospital";
            DataTable table = DBHelper.GetDataSet(safeSql);

            return table;
        }

        public static Hospital AddC_HospitalInfo(Hospital hospital)
        {
            string sql =
                "INSERT hospital (Name,jieshao)" +
                "VALUES (@Name,@jieshao)";

            sql += " ; SELECT @@IDENTITY";

            try
            {
                SqlParameter[] para = new SqlParameter[]
				{
                    new SqlParameter("@Name", hospital.Name),
                     new SqlParameter("@jieshao", hospital.Jieshao)

				};

                int newId = DBHelper.GetScalar(sql, para);
                return GetJigouById(newId);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }

        public static Hospital GetJigouById(int roleId)
        {
            string sql = "SELECT * FROM Hospital WHERE id = @id";

            try
            {
                SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@id", roleId));
                if (reader.Read())
                {
                    Hospital hospital = new Hospital();

                    hospital.ID = (int)reader["id"];

                    hospital.Name = (string)reader["name"];
                    hospital.Jieshao = (string)reader["Jieshao"];


                    reader.Close();

                    return hospital;
                }
                else
                {
                    reader.Close();
                    return null;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }

        public static DataTable GetHospitalInfo(string name)
        {
            string sql = "select  id ,  name ,jieshao from hospital  where 1=1 ";

            if (name != string.Empty)
            {
                sql += "" + " and name LIKE '%" + name + "%'";
            }

            return DBHelper.GetDataSet(sql);
        }


        public static void DeleteC_HospitalInfoByRoleId(int Id)
        {
            string sql = "DELETE hospital WHERE Id = @Id";

            try
            {
                SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@Id", Id)
				};

                DBHelper.ExecuteCommand(sql, para);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }


        /// <summary>
        /// 删除多条
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        /// 创建者：邹纳
        public static int DeleteC_HospitalInfoByRoleId(string Id)
        {
            string sql = "DELETE hospital WHERE id in (" + Id + ")";
            int liRowCount = -1;
            try
            {
                liRowCount = DBHelper.ExecuteCommand(sql);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
            return liRowCount;
        }

        public static void ModifyC_HospitalInfo(Hospital hospital)
        {
            string sql =
                "UPDATE hospital " +
                "SET " +
                    "Name = @Name, " +
                   
                    "jieshao = @jieshao " +

                "WHERE Id = @Id";


            try
            {
                SqlParameter[] para = new SqlParameter[]
				{
                     new SqlParameter("@Name", hospital.Name),
                     
                     new SqlParameter("@jieshao", hospital.Jieshao),
                     new SqlParameter("@Id", hospital.ID )
                   
				};

                DBHelper.ExecuteCommand(sql, para);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }

        }	
		
    }
}
